package org.dromara.system.utils;

import lombok.experimental.UtilityClass;
import org.dromara.common.redis.common.RedisConstants;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.vo.DataBaseVo;

import java.util.Date;

@UtilityClass
public class SystemUtils {

    public static DataBaseVo getSystemInfo() {
        DataBaseVo cacheObject = RedisUtils.getCacheObject(RedisConstants.SERVICE_PREFIX);
        if (cacheObject == null) {
            cacheObject = new DataBaseVo();
            initializeStatusInfos(cacheObject);
            RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, cacheObject);
        } else {
            // 确保从缓存中获取的对象中的状态信息已初始化
            initializeStatusInfos(cacheObject);
        }
        return cacheObject;
    }

    private static void initializeStatusInfos(DataBaseVo cacheObject) {
        if (cacheObject.getModbusTcpStatusInfo() == null) {
            cacheObject.setModbusTcpStatusInfo(new DataBaseVo.ModbusTcpStatusInfo());
        }
        if (cacheObject.getHttpServerStatusInfo() == null) {
            cacheObject.setHttpServerStatusInfo(new DataBaseVo.HttpServerStatusInfo());
        }
        if (cacheObject.getUdpServerStatusInfo() == null) {
            cacheObject.setUdpServerStatusInfo(new DataBaseVo.UdpServerStatusInfo());
        }
        if (cacheObject.getBsmServerStatusInfo() == null) {
            cacheObject.setBsmServerStatusInfo(new DataBaseVo.BsmServerStatusInfo());
        }

        // 设置默认值
        cacheObject.getModbusTcpStatusInfo().setName("综合管养");
        cacheObject.getModbusTcpStatusInfo().setStatus(1);
        cacheObject.getModbusTcpStatusInfo().setDesc("正常");
        cacheObject.getModbusTcpStatusInfo().setUpdateTime(new Date());

        cacheObject.getHttpServerStatusInfo().setName("渗漏水");
        cacheObject.getHttpServerStatusInfo().setStatus(1);
        cacheObject.getHttpServerStatusInfo().setDesc("正常");
        cacheObject.getHttpServerStatusInfo().setUpdateTime(new Date());

        cacheObject.getUdpServerStatusInfo().setName("应急电话");
        cacheObject.getUdpServerStatusInfo().setStatus(1);
        cacheObject.getUdpServerStatusInfo().setDesc("正常");
        cacheObject.getUdpServerStatusInfo().setUpdateTime(new Date());

        cacheObject.getBsmServerStatusInfo().setName("动环监测");
        cacheObject.getBsmServerStatusInfo().setStatus(1);
        cacheObject.getBsmServerStatusInfo().setDesc("正常");
        cacheObject.getBsmServerStatusInfo().setUpdateTime(new Date());


    }

    public static void setModbusTcpStatus(int status) {
        DataBaseVo dataBaseVo = getSystemInfo();
        DataBaseVo.ModbusTcpStatusInfo modbusTcpStatusInfo = dataBaseVo.getModbusTcpStatusInfo();
        modbusTcpStatusInfo.setStatus(status);
        modbusTcpStatusInfo.setUpdateTime(new Date());
        dataBaseVo.setModbusTcpStatusInfo(modbusTcpStatusInfo);
        RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, dataBaseVo);
    }

    public static void setHttpServerStatus(int status) {
        DataBaseVo dataBaseVo = getSystemInfo();
        DataBaseVo.HttpServerStatusInfo httpServerStatusInfo = dataBaseVo.getHttpServerStatusInfo();
        httpServerStatusInfo.setStatus(status);
        httpServerStatusInfo.setUpdateTime(new Date());
        dataBaseVo.setHttpServerStatusInfo(httpServerStatusInfo);
        RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, dataBaseVo);
    }

    public static void setUdpServerStatus(int status) {
        DataBaseVo dataBaseVo = getSystemInfo();
        DataBaseVo.UdpServerStatusInfo udpServerStatusInfo = dataBaseVo.getUdpServerStatusInfo();
        udpServerStatusInfo.setStatus(status);
        udpServerStatusInfo.setUpdateTime(new Date());
        dataBaseVo.setUdpServerStatusInfo(udpServerStatusInfo);
        RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, dataBaseVo);
    }

    public static void setBsmServerStatus(int status) {
        DataBaseVo dataBaseVo = getSystemInfo();
        DataBaseVo.BsmServerStatusInfo bsmServerStatusInfo = dataBaseVo.getBsmServerStatusInfo();
        bsmServerStatusInfo.setStatus(status);
        bsmServerStatusInfo.setUpdateTime(new Date());
        dataBaseVo.setBsmServerStatusInfo(bsmServerStatusInfo);
        RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, dataBaseVo);
    }

    public static void setYtServerStatus(int status) {
        DataBaseVo dataBaseVo = getSystemInfo();
        DataBaseVo.YtServerStatusInfo ytServerStatusInfo = dataBaseVo.getYtServerStatusInfo();
        ytServerStatusInfo.setStatus(status);
        ytServerStatusInfo.setUpdateTime(new Date());
        dataBaseVo.setYtServerStatusInfo(ytServerStatusInfo);
        RedisUtils.setCacheObject(RedisConstants.SERVICE_PREFIX, dataBaseVo);
    }
}
